package com;


import com.fz.entity.Student;
import com.fz.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by ZCY on 2018-01-16.
 */
public class Demo {
    @Test
    public void  ok(){
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("conf.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(is);
        SqlSession s = sf.openSession();
     /*   YangMapper yy = s.getMapper(YangMapper.class);
        System.out.println( yy.queryById(5));
        System.out.println("==========");
        List<Yang> lsit = yy.query();
        for(Yang p:lsit){
            System.out.println(p);
        }
*/
        StudentMapper sdao = s.getMapper(StudentMapper.class);
       // int i = sdao.save(18,"周小小");
        Student st = new Student();
        st.setAge(99);
        st.setName("赵九");

        int i = sdao.saveStudent(st);
        System.out.println(i);
        s.commit();


    }





    public void show(){
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("conf.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(is);
        SqlSession s = sf.openSession();
//       /*s删除数据*/
//        int in = s.delete("com.fz.mapper.StudentMapper.deleteid",2);
//        s.commit();/*提交一下，这是使用事务进行处理的*/
//        System.out.println("删除数据"+in);
//       /*c查询数据*/
//        List<Student> list = s.selectList("com.fz.mapper.StudentMapper.query");
//        System.out.println("查询数据"+list.size());
        /*查询一条数据*/
        // Student st = s.selectOne("com.fz.mapper.StudentMapper.queryById",6);
       // System.out.println("查询一条数据"+st);
//        Student sss = new Student();
//        /*更改数据*/
//        sss.setId(6);
//        sss.setAge(38);
//        sss.setName("王五五");
//        System.out.println(sss);
//        s.update("com.fz.mapper.StudentMapper.updata",sss);
//        s.commit();
        /*添加数据*/
     /*   Student stu = new Student();
        stu.setName("麻辣香锅");
        stu.setAge(199);
        s.insert("com.fz.mapper.StudentMapper.add",stu);
        s.commit();*/
     /*使用模糊查询*/
        List<Student> list  = s.selectList("com.fz.mapper.StudentMapper.queryLike","张%");
        System.out.println("使用模糊查询"+list.size());
        for(Student tt:list){
            System.out.println("使用模糊查询"+tt.toString());

        }







    }

}
